package com.example.myapplication.view;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import androidx.annotation.NonNull;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.myapplication.R;

/**
 * @author: rongfeng
 * @date: 2021/7/29
 * @description
 */
public class SendGiftCountDownView extends RelativeLayout {
    private View mView;
    private ProgressBar progressBar;
    private ValueAnimator animator;
    private String content = "连送%sS";
    private int downTime = 5;
    private TextView tv_count_time;
    private ObjectAnimator objectAnimatorX, objectAnimatorY;
    private AnimatorSet animatorSet;
    public SendGiftCountDownView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mView = LayoutInflater.from(context).inflate(R.layout.view_send_gift_coun_down, this);
        progressBar = mView.findViewById(R.id.pb_gift_count_down);
        tv_count_time = mView.findViewById(R.id.tv_count_time);
        animator = ValueAnimator.ofInt(1000, 0).setDuration(5000);
        animator.addUpdateListener(valueAnimator -> {
            int value = (int) valueAnimator.getAnimatedValue();
            progressBar.setProgress(value);
        });
        animator.setInterpolator(new LinearInterpolator());
        animatorSet = new AnimatorSet();
        objectAnimatorX = ObjectAnimator.ofFloat(mView, "scaleX", 0.8f, 1f);
        objectAnimatorY = ObjectAnimator.ofFloat(mView, "scaleY", 0.8f, 1f);
        animatorSet.setDuration(200);
        animatorSet.playTogether(objectAnimatorX, objectAnimatorY);
    }

    public void start() {
        setVisibility(VISIBLE);
        if (animator != null) {
            if (animator.isRunning()) {
                animator.cancel();
            }
            animator.start();

        }
        if (animatorSet.isRunning()) {
            animatorSet.cancel();
        }
        animatorSet.start();

        downTime = 5;
        mHandler.removeMessages(1);
        tv_count_time.setText(String.format(content, downTime));
        mHandler.sendEmptyMessageDelayed(1, 1000);
    }

    public void cancel() {
        if (animator != null) {
            animator.cancel();
        }
        if (animatorSet != null) {
            animatorSet.cancel();
        }
        mHandler.removeMessages(1);
    }

    Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                downTime--;
                if (downTime > 0) {
                    tv_count_time.setText(String.format(content, downTime));
                    mHandler.sendEmptyMessageDelayed(1, 1000);
                } else {
                    setVisibility(GONE);
                }
            }
        }
    };
}
